import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

import { getCurrentUser } from '@/api/user'
import { useRouter } from 'vue-router'

const router = useRouter();

export const useUserStore = defineStore('user', () => {
  const loginUser = ref<any>({
    username: '未登录',
  })

  async function fetchLoginUser() {
    try {
    　const user = await getCurrentUser();
      loginUser.value = user
    } catch (error) {
      router.push("/login")
      return undefined;
    }
  }

  function setLoginUser(user: any) {
    loginUser.value = user
  }

  return { loginUser, fetchLoginUser, setLoginUser}
})
